#!/usr/bin/python

import sys
import os

class configChecker():

    __Excute = None

    def __init__(self):
        self.__Excute=os.system
        return

    def check_device_match(self,ip,device):
        reader = os.popen("ssh "+ip+" fdisk -l|grep "+device)
        data = reader.readlines()
        if len(data) == 0:
            print "ERROR:"+ip+" no device name " + device
            sys.exit()
        if len(data) > 1:
            print "ERROR:"+ip+" device " + device + " has been used"
            sys.exit()
        
            
    def check_config(self,ips_worker,proxy,monitor,connector,dev):
        if dev['yum-device']:
            self.check_device_match(proxy,dev['yum-device'])
        if dev['swift-device']:
            self.check_device_match(proxy,dev['swift-device'])
        if dev['mongodb-device']:
            self.check_device_match(monitor,dev['mongodb-device'])
        if dev['mysql-device']:
            for work_ip in ips_worker:
                self.check_device_match(work_ip,dev['mysql-device'])
        if dev['mq-device']:
            for work_ip in ips_worker:
                self.check_device_match(work_ip,dev['mq-device'])
        if dev['connector-mongodb-device']:
            self.check_device_match(connector,dev['connector-mongodb-device'])
        return

    
if __name__ == "__main__":
    ips_worker = ["172.22.29.177","172.22.29.178"]
    proxy = "172.22.29.169"
    monitor = "172.22.29.168"
    connector = "172.22.29.173"
    dev = {}
    dev["yum-device"] = "/dev/sdc"
    dev["swift-device"] = "/dev/sdd"
    dev["mongodb-device"] = "/dev/sdc"
    dev["mysql-device"] = "/dev/sdc"
    dev["mq-device"] = "/dev/sdd"
    dev["connector-mongodb-device"] = "/dev/sdc"
    C = configChecker()
    C.check_config(ips_worker, proxy, monitor, connector, dev)
